<template>
  <div class="topbarout">
    <div class="topbar">
      <h1 id="weblogo">
        <img id="weblogo" src="../assets/images/LOGO.svg" alt="element-logo" class="nav-logo" style="height: 40px">
      </h1>
      <dl class="topbar-list">
        <dd id="firstPage" class="cur">
          <router-link :to="{ path: '/index' }" class="router-link-active">首页</router-link>
        </dd>
        <dd>
          <router-link :to="{ path: '/allMovies' }" class="router-link-active">电影</router-link>
          <!--          <a href="/dist/#/allMovies">电影</a>-->
        </dd>
        <dd>
          <router-link :to="{ path: '/community' }" class="router-link-active">社区</router-link>
        </dd>
        <dd>
          <a href="https://gitee.com/time_and_space_ferry/integrated-rec-sys">关于</a>
        </dd>
      </dl>
      <div class="tophead">
        <div class="topsearch">
          <div class="searchshadow">
            <div class="selectsearchtype">
              <div class="selectsearch">
                <el-select class="selectype" style="width: 90px;padding-left: 5px" v-model="searchtype" placeholder="全部">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <span>|</span>
              </div>
            </div>
            <div class="searcharea">
              <el-input class="borderNone" id="indexsearch" v-model="searchinfo" placeholder="搜索电影"
                onfocus="this.placeholder=''" onblur="this.placeholder='搜索电影'"></el-input>
            </div>
            <el-button type="primary" icon="el-icon-search" @click="search"></el-button>
          </div>
        </div>
      </div>
      <div class="headright">
        <div class="headtool publish" id="publish-btn"
          onmouseover="document.getElementById('submit-tip').style.display='block'"
          onmouseout="document.getElementById('submit-tip').style.display='none'">
          <a>
            <span class="publishbtn">发布</span>
          </a>
          <div class="headtip submit-tip" id="submit-tip" v-show="true">
            <p>
              <em class="article-icon"></em>
              <a href="#" @click="publishMovie">电影</a>
            </p>
          </div>
        </div>
        <div class="headtool" id="siginbox" v-if="!loginstatus"
          onmouseover="document.getElementById('loginbox').style.display='block'"
          onmouseout="document.getElementById('loginbox').style.display='none'">
          <!--            用户未登录，显示登录按钮-->
          <div class="headunlogin">
            <a href="#" class="cur">
              登录
            </a>
          </div>
          <!--            点击用户头像，弹出框-->
          <div id="loginbox" class="headtip login-tip new-headtip" style="z-index: 5; display: none;">
            <dl class="new-login-tool" style="z-index:5">
              <dt>
                <p class="gotoTel" @click="showRegister">注册</p>
                <p class="mailLogin active" @click="showLogin">密码登录</p>
                <div class="codeLogin"></div>
              </dt>
              <dd id="userlogin" class="userlogin" style="z-index:5">
                <ul style="z-index:5">
                  <li class="" id="useridbox" style="z-index:5"><input type="text" v-model:prefix-icon="user.userName"
                      value="输入用户名" placeholder="输入用户名" class="tip"></li>
                  <li class="" id="passwdbox" style="z-index:5"><input type="password"
                      v-model:prefix-icon="user.userPassword" placeholder="输入密码" data-login="password"></li>
                </ul>
                <p class="usertip" style="z-index:5">
                  <span class="loginWord"></span>
                </p>
                <div class="login-btn" style="{z-index:5}">
                  <p><el-button style="margin-left: 100px;width: 150px;height: 30px" type="primary" size="small"
                      autocomplete="off" @click="login" :disabled="!canSubmit" :loading="isLoding">登录</el-button></p>
                </div>
              </dd>
              <dd id="register" class="userlogin" style="z-index:5;display: none">
                <ul style="z-index:5">
                  <li class="" id="useridbox" style="z-index:5"><input type="text" v-model:prefix-icon="user.userName"
                      value="输入用户名" placeholder="输入用户名" class="tip"></li>
                  <li class="" id="passwdbox" style="z-index:5"><input type="password" v-model:prefix-icon="user.userPwd"
                      placeholder="输入密码" data-login="userPwd"></li>
                  <li class="" id="ConfirmPwd" style="z-index:5"><input type="password"
                      v-model:prefix-icon="user.userConfirmPwd" placeholder="确认密码" data-login="userConfirmPwd"></li>
                </ul>
                <p class="usertip" style="z-index:5">
                  <span class="loginWord"></span>
                </p>
                <div class="login-btn" style="{z-index:5}">
                  <p><el-button style="margin-left: 100px;width: 150px;height: 30px" type="primary" size="small"
                      autocomplete="off" @click="register" :disabled="!canRegister" :loading="isLoding">注册</el-button></p>
                </div>
              </dd>
            </dl>
          </div>
        </div>
        <!--         用户头像-->
        <!--        onmouseout="document.getElementById('userdetailinfo').style.display='none'"-->
        <div class="headtool" id="userinfobox" v-if="loginstatus"
          onmouseover="document.getElementById('userdetailinfo').style.display='block'"
          onmouseout="document.getElementById('userdetailinfo').style.display='none'">
          <div class="headlogin" id="headlogined">
            <el-avatar class="headphoto"
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
          </div>
          <div class="headtip user-tip" id="userdetailinfo" style="display: none">
            <div class="user-tip-first">
              <div class="user-tip-left">
                <p class="pic-box">
                  <el-avatar class="innerUserAvatar"
                    src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
                </p>
              </div>
              <div class="user-tip-right">
                <a v-if="">{{ userName }}</a>
              </div>
            </div>
            <div class="user-tip-second">
              <div class="userhistory"><a href="/dist/#/movieHistory?status=0"><span
                    style="font-size: 15px;font-weight: normal">看过</span></a></div>
              <p class="line" style="float:left;margin-top: 0px;margin-left: 20px"></p>
              <div class="userhistory"><a href="/dist/#/movieHistory?status=1"><span
                    style="font-size: 15px;font-weight: normal">收藏</span></a></div>
            </div>
            <div class="user-tip-last">
              <a class="btnlogout" @click="logout">退出登录</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "FontHeader",
  data() {
    return {
      user: {
        // userName: "world",
        // userPassword: "123456abc"
      },
      userName: '',
      userlogout: {},
      isLoding: false,
      placetext: "搜索电影",
      searchinfo: "",
      options: [{
        value: '0',
        label: '电影名称'
      }, {
        value: '1',
        label: '电影类型'
      }],
      //value为el-select当前被选中的el-option的value属性值
      searchtype: "",
      movies: [],
      loginstatus: 0,
      show3: false,
      publishdisplay: false,
      page: 1,
      pageSize: 5,
    }
  },
  methods: {
    islogin() {
      //这样做只能检测到有无token，却不能检测到token是否过期
      if (localStorage.getItem("Token") !== null) {
        this.loginstatus = 1
        this.userName = localStorage.getItem("userName")
      } else {
        this.loginstatus = 0
      }
    },
    login() {
      this.isLoding = true
      //在提交请求前先对form进行校验
      this.request.post("/user/visitor/userLogin", this.user).then(res => {
        if (res.code == 0) {
          this.userName = res.data.userName
          localStorage.setItem('Token', res.data.tokenInfo.accessToken)
          localStorage.setItem('userId', res.data.tokenInfo.userId)
          localStorage.setItem('userName', res.data.userName)
          localStorage.setItem('userRatingNum', res.data.userRatingNum)
          this.loginstatus = 1
          this.$message.success("登陆成功")
          this.$emit('reload')
        } else {
          this.$message.error(res.message)
          this.isLoding = false
        }
      })
    },
    logout() {
      this.userlogout.userId = localStorage.getItem("userId")
      this.userlogout.token = localStorage.getItem("Token")
      this.request.post("/user/userLogOut", this.userlogout).then(res => {
        if (res.code == 0) {
          this.$message.success(res.message)
          this.loginstatus = 0
          localStorage.clear()
          this.reload()
        } else {
          this.$message.error(res.message)
          localStorage.clear()
          this.reload()
        }
      })
    },
    search() {
      console.log(this.searchtype)
      if (this.searchtype == "") {
        this.$message.error("请输入查询类型")
        return
      }
      this.$router.push({
        name: 'MovieList',
        query: {
          page: this.page,
          pageSize: this.pageSize,
          searchType: this.searchtype,
          info: this.searchinfo
        }
      })
    },
    showRegister() {
      document.getElementById("register").style.display = 'block'
      document.getElementById("userlogin").style.display = 'none'
    },
    showLogin() {
      document.getElementById("userlogin").style.display = 'block'
      document.getElementById("register").style.display = 'none'
    },
    register() {
      this.isLoding = true
      //在提交请求前先对form进行校验
      this.request.post("/manager/visitor/userRegister", this.user).then(res => {
        if (res.code == 0) {
          this.$message.success("注册成功")
          this.isLoding = false
          // this.$emit('reload')
        } else {
          this.$message.error(res.message)
          this.isLoding = false
        }
      })
    },
    publishMovie() {
      this.$message.info("该功能暂未启用")
      return
    }
  },
  created() {
    this.islogin()
  },
  computed: {
    canSubmit() {
      const { userName, userPassword } = this.user
      return Boolean(userName && userPassword)
    },
    canRegister() {
      const { userName, userPwd, userConfirmPwd } = this.user
      return Boolean(userName && userPwd && userConfirmPwd)
    }
  },
}
</script>

<style scoped></style>